package com.zbkj.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.common.constants.CouponConstants;
import com.zbkj.common.constants.DateConstants;
import com.zbkj.common.model.MemberRewards;
import com.zbkj.common.model.coupon.Coupon;
import com.zbkj.common.model.coupon.CouponUser;
import com.zbkj.common.model.member.PaidMemberCard;
import com.zbkj.common.model.order.RechargeOrder;
import com.zbkj.common.model.user.User;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.service.dao.*;
import com.zbkj.service.service.CouponUserService;
import com.zbkj.service.service.MemberRewardsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author keNan
 * @description MemberRewardsServiceImpl 接口实现
 * @date 2025-04-10
 */
@Service
public class MemberRewardsServiceImpl extends ServiceImpl<MemberRewardsDao, MemberRewards> implements MemberRewardsService {


    @Autowired
    private RechargeOrderDao rechargeOrderDao;
    @Autowired
    private PaidMemberCardDao paidMemberCardDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private MemberRewardsDao memberRewardsDao;
    @Autowired
    private CouponDao couponDao;
    @Autowired
    private CouponUserService couponUserService;


    /**
     * 查询会员奖励并发放奖励
     *
     * @param rechargeOrder
     */
    @Override
    public void sendMemberReward(RechargeOrder rechargeOrder) {
        List<PaidMemberCard> list = paidMemberCardDao.selectAllList();
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        User user = userDao.selectById(rechargeOrder.getUid());
        //历史充值总金额,不包括这次充值的金额
        BigDecimal historyPrice = rechargeOrderDao.selectRechargePriceByUid(rechargeOrder.getUid(), rechargeOrder.getOrderNo());
        historyPrice = historyPrice == null ? BigDecimal.ZERO : historyPrice;
        //包括这次充值的总金额
        BigDecimal totalPrice = historyPrice.add(rechargeOrder.getPrice());

        for (int i = 0; i < list.size(); i++) {
            PaidMemberCard paidMemberCard = list.get(i);
            if (totalPrice.compareTo(paidMemberCard.getPrice()) > -1 && user.getPaidMemberCardId() != paidMemberCard.getId()) {
                Integer paidMemberCardId = list.get(i).getId();
                MemberRewards m = memberRewardsDao.selectOneByPaidMemberCardId(paidMemberCardId);
                if (m != null && m.getCouponId() != null) {
                    Coupon coupon = couponDao.selectById(m.getCouponId());
                    CouponUser couponUser = new CouponUser();
                    couponUser.setCouponId(m.getCouponId());
                    couponUser.setMerId(coupon.getMerId());
                    couponUser.setUid(user.getId());
                    couponUser.setName(coupon.getName());
                    couponUser.setPublisher(coupon.getPublisher());
                    couponUser.setCategory(coupon.getCategory());
                    couponUser.setReceiveType(coupon.getReceiveType());
                    couponUser.setCouponType(coupon.getCouponType());
                    couponUser.setMoney(coupon.getMoney());
                    couponUser.setDiscount(coupon.getDiscount());
                    couponUser.setMinPrice(coupon.getMinPrice());
                    couponUser.setStatus(CouponConstants.STORE_COUPON_USER_STATUS_USABLE);
                    //是否有固定的使用时间
                    if (!coupon.getIsFixedTime()) {
                        String endTime = CrmebDateUtil.addDay(CrmebDateUtil.nowDate(DateConstants.DATE_FORMAT), coupon.getDay(), DateConstants.DATE_FORMAT);
                        couponUser.setStartTime(CrmebDateUtil.nowDateTimeReturnDate(DateConstants.DATE_FORMAT));
                        couponUser.setEndTime(CrmebDateUtil.strToDate(endTime, DateConstants.DATE_FORMAT));
                    } else {
                        couponUser.setStartTime(coupon.getUseStartTime());
                        couponUser.setEndTime(coupon.getUseEndTime());
                    }
                    List<CouponUser> add = new ArrayList<>();
                    for (int j = 0; j < m.getCouponNumber(); j++) {
                        add.add(couponUser);
                    }
                    couponUserService.saveBatch(add);
                }
                user.setPaidMemberCardId(paidMemberCardId);
                return;
            }
        }
    }





}
